{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Problems with your probability_uniform Function\n",
    "\n",
    "Your `probability_uniform` function should work at this point. But, you might run into a couple of problems. \n",
    "\n",
    "1. What happens if you call your function like this: `probability_range(35, 20, 0, 360)` ?\n",
    "2. What happens if you input an angle that is outside the possibilities of the bottle's possible outcomes like probability_range(-25, 390, 0, 360)?\n",
    "3. What if you call your function like this: probability_range('a', 'b', 0, 360)?\n",
    "\n",
    "When writing functions, it's important to think of edge cases or incorrect user input. \n",
    "\n",
    "# Your Task\n",
    "\n",
    "Your task is to improve your function. Let's make these changes one step at a time. \n",
    "\n",
    "# Task One\n",
    "\n",
    "Make sure the function outputs a valid probability when low_range is greater than high_range.\n",
    "\n",
    "There is more than one way to go about this. You could:\n",
    "1. Calculate the absolute value of high_range - low_range \n",
    "2. Use if statements to compare low_range and high_range to see which one is greater\n",
    "\n",
    "Here is a [link](https://docs.python.org/2/library/functions.html#abs) to using Python's absolute value function.\n",
    "\n",
    "Fill out the TODOs below."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def probability_range_improved(low_range, high_range, minimum, maximum):\n",
    "\n",
    "    # TODO: calulate and return the probability \n",
    "    # even if low range is greater than high range.\n",
    "    # Use the abs() function or if statements\n",
    "    # 考虑 low_range 大于 high_range 的情况的处理\n",
    "    probability = 0\n",
    "    #  a - b = -(b - a) 也就是说2者相同 a - b 与 b - a 的 abs 相等\n",
    "    probability = abs (high_range - low_range) * 1.0 / (maximum - minimum)\n",
    "    return probability"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Run the cell below to see if your function works as expected."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Nice work!\n"
     ]
    }
   ],
   "source": [
    "assert \"{0:.2f}\".format(probability_range_improved(25, 700, 5, 800)) == '0.85'\n",
    "assert \"{0:.2f}\".format(probability_range_improved(700, 25, 5, 800)) == '0.85'\n",
    "print('Nice work!')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Task Two\n",
    "\n",
    "Check the inputs to the function to make sure they are not strings. If the user inputted a string, the function should return None. \n",
    "\n",
    "This exercise might seem trivial, but if you try to do something like `'my_string'/2` in Python, you will get an error. Debugging the errors and avoiding them is a key programming skill. \n",
    "\n",
    "Hint: Use the Python isinstance() function. If you're not sure how to use this function, search for it online.\n",
    "\n",
    "Hint: Use your code from Task One to calculate the probability"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "def probability_range_improved(low_range, high_range, minimum, maximum):\n",
    "\n",
    "    # TODO: check if any of the inputs are strings.\n",
    "    # hint: the python function isinstance() will be useful\n",
    "    if (isinstance(low_range, str)):\n",
    "        # print a message to the user and return none\n",
    "        print('Inputs should be numbers not string')\n",
    "        return None\n",
    "    \n",
    "    probability = 0\n",
    "    return probability\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Run the next cell to check your results."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Inputs should be numbers not string\n"
     ]
    },
    {
     "ename": "AssertionError",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAssertionError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-9-da98e5a1a354>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[1;31m# assert probability_range_improved(5, 'b', -100, 500) == None\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      3\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 4\u001b[1;33m \u001b[1;32massert\u001b[0m \u001b[1;34m\"{0:.2f}\"\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mprobability_range_improved\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m25\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m700\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m5\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m800\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;34m'0.85'\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      5\u001b[0m \u001b[1;32massert\u001b[0m \u001b[1;34m\"{0:.2f}\"\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mprobability_range_improved\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m700\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m25\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m5\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m800\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;34m'0.85'\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      6\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mAssertionError\u001b[0m: "
     ]
    }
   ],
   "source": [
    "assert probability_range_improved('a', 0, -100, 500) == None\n",
    "# assert probability_range_improved(5, 'b', -100, 500) == None\n",
    "\n",
    "assert \"{0:.2f}\".format(probability_range_improved(25, 700, 5, 800)) == '0.85'\n",
    "assert \"{0:.2f}\".format(probability_range_improved(700, 25, 5, 800)) == '0.85'\n",
    "\n",
    "print('Well done!')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Task Three\n",
    "\n",
    "Check that the user has only inputted low_range and high_range values that are in between the allowed minimum and maximum. If an input is out of the allowed range, return None. \n",
    "\n",
    "Hint: Use your code from Task One to calculate the probability"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def probability_range_improved(low_range, high_range, minimum, maximum):\n",
    "    \n",
    "    # TODO check that low_range is between minimum and maximum\n",
    "    if ():\n",
    "        # print a message to the user and return none\n",
    "        print('Your low range value must be between minimum and maximum')\n",
    "        return None\n",
    "        \n",
    "    # TODO check that high_range is between min and max\n",
    "    if ():\n",
    "        # print a message to the user and return none\n",
    "        print('The high range value must be between minimum and maximum')\n",
    "        return None\n",
    "\n",
    "    probability = 0\n",
    "    return probability\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Run the next code cell to check your results."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "assert probability_range_improved(-100, 300, 100, 500) == None\n",
    "assert probability_range_improved(105, 700, 100, 500) == None\n",
    "\n",
    "assert \"{0:.2f}\".format(probability_range_improved(25, 700, 5, 800)) == '0.85'\n",
    "assert \"{0:.2f}\".format(probability_range_improved(700, 25, 5, 800)) == '0.85'\n",
    "\n",
    "print('Awesome!')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Task Four\n",
    "\n",
    "Take all of your work from task one, two and three. Put them into one final function."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def probability_range_improved(low_range, high_range, minimum, maximum):\n",
    "\n",
    "    # TODO: check if any of the inputs are strings.\n",
    "    # hint: the python function isinstance() will be useful\n",
    "    if ( isinstance(low_range, str) or isinstance(high_range, str)):\n",
    "        # print a message to the user and return none\n",
    "        print('Inputs should be numbers not string')\n",
    "        return None\n",
    "    \n",
    "    # TODO check that low_range is between minimum and maximum\n",
    "    if ():\n",
    "        # print a message to the user and return none\n",
    "        print('Your low range value must be between minimum and maximum')\n",
    "        return None\n",
    "        \n",
    "    # TODO check that high_range is between min and max\n",
    "    if ():\n",
    "        # print a message to the user and return none\n",
    "        print('The high range value must be between minimum and maximum')\n",
    "        return None\n",
    "\n",
    "    # TODO: calulate and return the probability \n",
    "    # even if low range is greater than high range\n",
    "    probability = 0\n",
    "    return probability\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Run the cell below. If there are no AssertionErrors, then your code runs as expected. In Python, assert checks whether a statement\n",
    "resolves to True or False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "assert probability_range_improved('a', 0, -100, 500) == None\n",
    "assert probability_range_improved(0, 'b', -100, 500) == None\n",
    "assert probability_range_improved(-100, 300, 100, 500) == None\n",
    "assert probability_range_improved(105, 700, 100, 500) == None\n",
    "assert \"{0:.2f}\".format(probability_range_improved(25, 700, 5, 800)) == '0.85'\n",
    "assert \"{0:.2f}\".format(probability_range_improved(700, 25, 5, 800)) == '0.85'\n",
    "print('You got the results we were looking for!')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
